package com.baturu.vin.ford.dal.dao;

import com.baturu.vin.dto.CarTypeBuilderDTO;
import com.baturu.vin.ford.dal.entity.FordBuildCatalogEntity;
import com.baturu.vin.ford.dal.entity.FordOptionInfoEntity;
import com.baturu.vin.ford.dal.entity.FordSectionInfoEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface FordCommonDAO {

    @Select("select DISTINCT `attributeCode`, `attributeLabel` from ford_option ")
    Set<Map<String, String>> queryAttributeAll();

    @Select({"select distinct enLabel, chLabel from ford_version_translate"})
    List<Map<String, String>> queryVersionTranslate();

    @Select({"select distinct description, color, feature from ford_color_feature_mapping"})
    List<Map<String, String>> queryColorFeatureMapping();

    /**
     * 新增关联信息
     */
    @Insert("INSERT IGNORE INTO `ford_btr_relation` (`btrId`, `optionKey`, `modelYear`) VALUES " +
            "(#{btrId}, #{optionKey}, #{modelYear})")
    void saveBtrRelation(@Param("btrId") String btrId, @Param("optionKey") String optionKey, @Param("modelYear") String modelYear);

    @Select("select `attributeCode`, `code` from ford_option ")
    List<Map<String, String>> queryAllOption();

    @Insert({"<script>" +
            "INSERT IGNORE INTO `ford_option` " +
            "(`attributeCode`, `attributeLabel`, `code`, `englishLabel`, `label`) " +
            "<foreach item='option' index='index' collection='options' open='VALUES ' separator=',' close=' '>" +
            "(#{option.attributeCode},#{option.attributeLabel}, #{option.code}, #{option.englishLabel}, #{option.label})" +
            "</foreach>" +
            "</script>"})
    void saveOption(@Param("options") List<Map<String, Object>> options);

    @Select({"select `enName`, `code`, `brand`, `subBrand`, `carSystemId`, `carSystem`, `year` ",
            "from build_catalog ",
            "where code = #{catalogCode} and madeInChina = #{madeInChina} "})
    FordBuildCatalogEntity queryBuildCatalogByCode(@Param("catalogCode") String catalogCode, @Param("madeInChina") boolean madeInChina);

    @Select({"<script>" +
            "select btrId from ford_btr_relation where optionKey = #{optionKey} " +
            "<if test='modelYear != null'>" +
            "and modelYear = #{modelYear} " +
            "</if>" +
            "limit 1 " +
            "</script>"})
    String queryBtrRelation(@Param("optionKey") String optionKey, @Param("modelYear") String modelYear);

    @Select({"<script>",
            "select attributeCode, code, UPPER(label) label FROM ford_option ",
            "WHERE  (attributeCode, code) IN ",
            "<foreach item='item' index='index' collection='options' open='(' separator=',' close=') '>",
            "(#{item.attribute}, #{item.code})",
            "</foreach>",
            "</script>"})
    Set<FordOptionInfoEntity> queryMajorOptionByCode(@Param("options") List<Map<String, String>> options);

    @Select({"<script>",
            "select attributeCode, code, UPPER(label) label FROM ford_option ",
            "WHERE attributeCode in ('minor', 'speccodes') ",
            "AND `code` IN ",
            "<foreach item='item' index='index' collection='codes' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            "</script>"})
    Set<FordOptionInfoEntity> queryMinorOptionByCode(@Param("codes") Set<String> codes);

    @Select({"<script>",
            "select distinct attributeCode, UPPER(label) label FROM ford_option ",
            "WHERE  (attributeCode, code) IN ",
            "<foreach item='item' index='index' collection='options' open='(' separator=',' close=') '>",
            "(#{item.attribute}, #{item.code})",
            "</foreach>",
            "</script>"})
    List<Map<String, String>> queryOptionLabelByCode(@Param("options") List<Map<String, String>> options);

    @Select({"select distinct UPPER(label) label from ford_option",
            "where attributeCode = 'W'"})
    List<String> queryOptionLabelForArea();

    @Select({"<script>",
            "select catalogCode, `data` as code, UPPER(label) label FROM ford_section ",
            "WHERE (catalogCode, `data`) IN ",
            "<foreach item='item' index='index' collection='sections' open='(' separator=',' close=') '>",
            "(#{item.catalogCode}, #{item.sectionCode})",
            "</foreach>",
            "</script>"})
    Set<FordSectionInfoEntity> querySectionByAttributeAndCode(@Param("sections") List<Map<String, String>> sections);


    @Insert("<script>" +
            "INSERT INTO qp_cartype " +
            "(BTRCountry,BTRID, Brand, BTRBrand, BTRSubBrand, " +
            "Manufacturers, CarSystemId, CarSystem, CarType, SalesName, " +
            "MJ, ProductTime, Vcode, EngineType, EngineDesc, " +
            "Displacement, BodyModel, AirIntake, CylinderVolume, GearsNum, " +
            "DriveMode, SalesVersion, TransmissionType, TransmissionDesc, Chassis, " +
            "ShowCarType, onlineflag, diyStatus, completed, `status`, " +
            "vehicleType, driveType, doorNumber, seats, " +
            "createTime, doUser, doTime) values " +
            "<foreach item='carType' index='index' collection='carTypes' open=' ' separator=',' close=' '>" +
            "(#{carType.btrCountry},#{carType.btrId}, #{carType.brand}, #{carType.btrBrand}, #{carType.btrSubBrand}, " +
            "#{carType.manufacturers}, #{carType.carSystemId}, #{carType.carSystem}, #{carType.carType}, #{carType.salesName}, " +
            "#{carType.mj}, #{carType.productTime},#{carType.vcode}, #{carType.engineType}, #{carType.engineDesc}, " +
            "#{carType.displacement}, #{carType.bodyModel}, #{carType.airIntake}, #{carType.cylinderVolume}, #{carType.gearsNum}, " +
            "#{carType.driveMode},#{carType.salesVersion}, #{carType.transmissionType}, #{carType.transmissionDesc}, #{carType.chassis}, " +
            "#{carType.showCarType}, 1, 2, 0, 1, " +
            "#{carType.vehicleType}, #{carType.driveType}, #{carType.doorNumber}, #{carType.seats}, " +
            "now(), '', now())" +
            "</foreach>" +
            "</script>")
    void saveNewCarType(@Param("carTypes") List<CarTypeBuilderDTO> carTypes);

    @Select("select distinct vin from ford_vin_info")
    List<String> queryAllVin();

    @Select({"SELECT en from ford_epc_text WHERE idx=#{idx}"})
    String queryTextByIdx(@Param("idx") String idx);
}